<?php

namespace app\forms\user;

use app\models\User;
use app\components\helpers\ArrayHelper;

class CreateForm extends BaseForm
{
    public function rules()
    {
        $rules = parent::rules();
        return array_merge($rules, [
            ['number', 'required'],
            ['realName', 'required'],
            ['username', 'required'],
            ['password', 'required'],
            ['mobile', 'required'],
            ['sex', 'required'],
            ['groupId', 'required'],
            ['deptId', 'required'],
            ['positionId', 'required'],
        ]);
    }

    /**
     * 创建用户
     */
    public function createUser()
    {
        $attr = $this->getAttributes();
        $attr = ArrayHelper::humpToUnderline($attr);

        $keys = ['number', 'username', 'password'];
        $user = array_intersect_key($attr, array_flip($keys)); // 主表
        $profile = array_diff_key($attr, array_flip($keys)); // 附表
        $roleArr = $profile['role_id']; // 角色
        unset($profile['role_id']);
        return User::performCreate($user, $profile, $roleArr);
    }
}